package com.rzn.reading.service.impl;

import com.rzn.reading.BeanMapper;
import com.rzn.reading.entity.Attach;
import com.rzn.reading.entity.LiteratureDetail;
import com.rzn.reading.entity.Rank;
import com.rzn.reading.entity.ReadingProgress;
import com.rzn.reading.mapper.AttachMapper;
import com.rzn.reading.mapper.LiteratureDetailMapper;
import com.rzn.reading.mapper.RankMapper;
import com.rzn.reading.mapper.ReadingProgressMapper;
import com.rzn.reading.model.dto.literature.LiteratureItemListDTO;
import com.rzn.reading.model.dto.literature.LiteratureTypeGradeDTO;
import com.rzn.reading.model.dto.readingProgress.IsReadingLiteratureDTO;
import com.rzn.reading.model.dto.readingProgress.ReadingProgressCreateInputDTO;
import com.rzn.reading.model.dto.readingProgress.UserReadingProgressDTO;
import com.rzn.reading.service.LiteraturedetailService;
import com.rzn.reading.service.ReadingProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 阅读进度表 服务实现类
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@Service
public class ReadingProgressServiceImpl implements ReadingProgressService {
    @Autowired
    private ReadingProgressMapper readingProgressMapper;

    @Autowired
    private AttachMapper attachMapper;

    @Autowired
    private RankMapper rankMapper;

    @Override
    public Boolean deleteLiteratureProgress(Integer id,Integer state) {
        return readingProgressMapper.updateLiteratureProgressState(id,state) == 1;
    }

    @Override
    public UserReadingProgressDTO getUserLiteratureProgress(Integer userId, Integer literatureId) {
        ReadingProgress readingProgress = readingProgressMapper.selectByUserIdAndLiteratureId(userId, literatureId);
        UserReadingProgressDTO readingProgressDTO = BeanMapper.map(readingProgress, UserReadingProgressDTO.class);
        return readingProgressDTO;
    }

    @Override
    public Boolean updateUserReadingProgress(UserReadingProgressDTO userReadingProgressDTO) {
        ReadingProgress readingProgress = BeanMapper.map(userReadingProgressDTO, ReadingProgress.class);
        readingProgress.setState(0);
        return readingProgressMapper.updateByPrimaryKey(readingProgress) == 1;
    }

    @Override
    public Boolean insertUserReadingProgress(ReadingProgressCreateInputDTO readingProgressCreateInputDTO) {
        ReadingProgress map = BeanMapper.map(readingProgressCreateInputDTO, ReadingProgress.class);
        map.setState(0);
        return readingProgressMapper.insert(map) == 1;
    }

    @Override
    public List<IsReadingLiteratureDTO> getIsReadingLiterature(Integer userId) {
        List<Rank> ranks = rankMapper.selectAll();
        List<Attach> attaches = attachMapper.selectByOwnerName("literature_detail");
        List<IsReadingLiteratureDTO> isReadingLiterature = readingProgressMapper.getIsReadingLiterature(userId);
        isReadingLiterature.forEach(isReadingLiteratureDTO -> {
            List<Rank> list = ranks.stream().filter(x -> x.getLiteratureId() == isReadingLiteratureDTO.getLiteratureId()).collect(Collectors.toList());
            Double avg = list.stream().map(Rank::getGrade).collect(Collectors.averagingDouble(Integer::doubleValue));
            isReadingLiteratureDTO.setGrade(avg);
            Optional<Attach> first = attaches.stream().filter(x -> x.getOwnerId() == isReadingLiteratureDTO.getLiteratureId()).findFirst();
            if (first.isPresent()) {
                isReadingLiteratureDTO.setUrl(first.get().getUrl());
            }
        });
        return isReadingLiterature;
    }
}
